package com.senney.practice.mutilThread.demo02;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 
 * @author Lanmengye@outlook.com
 * @date 2020-07-19
 * @Description Lock接口使用示例
 */
public class LockDemo {

	public static void main(String[] args) {

		Ticket tick = new Ticket();
		new Thread(tick, "窗口1").start();
		new Thread(tick, "窗口2").start();
		new Thread(tick, "窗口3").start();
	}
}

class Ticket implements Runnable {

	private int count = 1000;
	private Lock lock = new ReentrantLock();

	@Override
	public void run() {
		while (true) {
			lock.lock();
			try {
				if (count > 0) {
					try {
						Thread.sleep(5);
					} catch (InterruptedException e) {
					}
					System.out.println(Thread.currentThread().getName() + "完成售票，余票为：" + --count);
				}
			} finally {
				lock.unlock();
			}

		}
	}

}